using System;

namespace ICP_MS.Plugin.HPLC;

public class Complex
{
	private double real;

	private double image;

	public double Real
	{
		get
		{
			return real;
		}
		set
		{
			real = value;
		}
	}

	public double Image
	{
		get
		{
			return image;
		}
		set
		{
			image = value;
		}
	}

	public Complex()
		: this(0.0, 0.0)
	{
	}

	public Complex(double real)
		: this(real, 0.0)
	{
	}

	public Complex(double real, double image)
	{
		this.real = real;
		this.image = image;
	}

	public static Complex operator +(Complex c1, Complex c2)
	{
		return new Complex(c1.real + c2.real, c1.image + c2.image);
	}

	public static Complex operator -(Complex c1, Complex c2)
	{
		return new Complex(c1.real - c2.real, c1.image - c2.image);
	}

	public static Complex operator *(Complex c1, Complex c2)
	{
		return new Complex(c1.real * c2.real - c1.image * c2.image, c1.image * c2.real + c1.real * c2.image);
	}

	public double ToModul()
	{
		return Math.Sqrt(real * real + image * image);
	}

	public override string ToString()
	{
		if (Real == 0.0 && Image == 0.0)
		{
			return $"{0}";
		}
		if (Real == 0.0 && Image != 1.0 && Image != -1.0)
		{
			return $"{Image} i";
		}
		if (Image == 0.0)
		{
			return $"{Real}";
		}
		if (Image == 1.0)
		{
			return $"i";
		}
		if (Image == -1.0)
		{
			return $"- i";
		}
		if (Image < 0.0)
		{
			return $"{Real} - {0.0 - Image} i";
		}
		return $"{Real} + {Image} i";
	}
}
